package com.dingwei.superiordiary.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.util.AbFileUtil;
import com.ab.util.AbImageUtil;
import com.ab.view.cropimage.CropImage;
import com.ab.view.cropimage.CropImageView;
import com.allen.library.SuperButton;
import com.dingwei.superiordiary.R;
import com.lihao.baselibrary.base.BaseActivity;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 剪切界面
 */
@SuppressLint("ShowToast")
public class CropImageActivity extends BaseActivity implements OnClickListener {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.crop_image)
    CropImageView cropImage;
    @Bind(R.id.rotateLeft)
    Button rotateLeft;
    @Bind(R.id.cancelBtn)
    SuperButton cancelBtn;
    @Bind(R.id.okBtn)
    SuperButton okBtn;
    @Bind(R.id.rotateRight)
    Button rotateRight;
    @Bind(R.id.layout_bottom)
    LinearLayout layoutBottom;
    // private static final boolean D = Constant.DEBUG;
    private Bitmap mBitmap;

    private CropImage mCrop;

    private String mPath = "CropImageActivity";
    public int screenWidth = 0;
    public int screenHeight = 0;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    break;

            }

        }
    };


    @Override
    protected int getContentViewId() {
        return R.layout.crop_image;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        init();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mBitmap != null) {
            mBitmap = null;
        }
    }

    @SuppressLint("ShowToast")
    private void init() {

        tvTitle.setText("剪裁图片");
        imgBack.setVisibility(View.GONE);
        getWindowWH();
        mPath = getIntent().getStringExtra("PATH");
        okBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        rotateLeft.setOnClickListener(this);
        rotateRight.setOnClickListener(this);


        // 相册中原来的图片
        File mFile = new File(mPath);
        try {
            mBitmap = AbFileUtil.getBitmapFromSD(mFile, AbImageUtil.SCALEIMG, 500, 500);
            if (mBitmap == null) {
                showToast("没有找到图片");
                finish();
            } else {
                resetImageView(mBitmap);
            }
        } catch (Exception e) {
            showToast("没有找到图片");
            finish();
        }
    }

    /**
     * 获取屏幕的高和宽
     */
    private void getWindowWH() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    private void resetImageView(Bitmap b) {
        cropImage.clear();
        cropImage.setImageBitmap(b);
        cropImage.setImageBitmapResetBase(b, true);
        mCrop = new CropImage(this, cropImage, mHandler);
        mCrop.crop(b);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancelBtn:
                finish();
                break;
            case R.id.okBtn:
                String path = mCrop.saveToLocal(mCrop.cropAndSave());
                Intent intent = new Intent();
                intent.putExtra("PATH", path);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.rotateLeft:
                mCrop.startRotate(270.f);
                break;
            case R.id.rotateRight:
                mCrop.startRotate(90.f);
                break;

        }
    }

}
